package com.jair.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jair.dao.impl.ScoresDaoImpl;
import com.jair.domain.Scores;
import com.jair.tools.CellUtils;



@Service
public class ScoresServiceImpl{
	@Autowired
	private ScoresDaoImpl scoresDao;
	
	
	public void saveScores(Scores scores){
		this.scoresDao.save(scores);
	}
	
	public List<Scores> parserExcelToScore(Workbook wb){
		if(wb == null){
			return null;
		}
		List<Scores> scoresList = new ArrayList<Scores>();
		for (int k = 0; k < wb.getNumberOfSheets(); k++) {
			Sheet sheet = wb.getSheetAt(k);
			int rows = sheet.getPhysicalNumberOfRows();
			System.out.println("Sheet " + k + " \"" + wb.getSheetName(k) + "\" has " + rows
					+ " row(s).");
			for (int r = 0; r < rows; r++) {
				Row row = sheet.getRow(r);
				if (row == null) {
					continue;
				}
				if(CellUtils.getCellValue(row.getCell(0)) == null || CellUtils.getCellValue(row.getCell(0)) == ""){
					continue;
				}
				Scores scores = new Scores(wb.getSheetName(k), 
						CellUtils.getCellValue(row.getCell(0)), 
						CellUtils.getCellValue(row.getCell(1)),
						Integer.valueOf(CellUtils.getCellValue(row.getCell(2))),
						Integer.valueOf(CellUtils.getCellValue(row.getCell(3))),
						Integer.valueOf(CellUtils.getCellValue(row.getCell(4))),
						Integer.valueOf(CellUtils.getCellValue(row.getCell(5))),
						Integer.valueOf(CellUtils.getCellValue(row.getCell(6))),
						Integer.valueOf(CellUtils.getCellValue(row.getCell(7))),
						Integer.valueOf(CellUtils.getCellValue(row.getCell(8))),
						Integer.valueOf(CellUtils.getCellValue(row.getCell(9))),
						Integer.valueOf(CellUtils.getCellValue(row.getCell(10))),
						Integer.valueOf(CellUtils.getCellValue(row.getCell(11))),
						Integer.valueOf(CellUtils.getCellValue(row.getCell(12))));
				scoresList.add(scores);
			}
		}
        return scoresList;
		
	}
}
